package com.homework;

import java.io.IOException;
import java.net.*;

public class SendB {
    public static void main(String[] args) throws IOException {
        //创建对象
        DatagramSocket datagramSocket=new DatagramSocket(8848);
        byte[] book="四大名著是哪些?".getBytes();
        DatagramPacket datagramPacket=new DatagramPacket(book,book.length, InetAddress.getLocalHost(),7777);
        //发送数据
        datagramSocket.send(datagramPacket);

        //接收数据并读取
        byte []data=new byte[1024];
        DatagramPacket datagramPacket2=new DatagramPacket(data,data.length);
        datagramSocket.receive(datagramPacket2);

        //取数据
        byte []bytes=datagramPacket2.getData();
        int len=datagramPacket2.getLength();
        //输出数据
        System.out.println(new String(bytes,0,len));


        //关闭
        datagramSocket.close();


    }
}
